package com.furnitur.modules.portal.controller.auth;

import com.furnitur.common.api.CommonResult;
import com.furnitur.modules.portal.dto.auth.PortalLoginDTO;
import com.furnitur.modules.portal.dto.auth.PortalRegisterDTO;
import com.furnitur.modules.portal.service.auth.PortalAuthService;
import com.furnitur.modules.portal.vo.auth.PortalUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/portal/auth")
public class PortalAuthController {

    @Autowired
    private PortalAuthService portalAuthService;

    @PostMapping("/register")
    public CommonResult<PortalUserVO> register(@Validated @RequestBody PortalRegisterDTO registerDTO) {
        log.info("Portal user register: {}", registerDTO.getUsername());
        PortalUserVO userVO = portalAuthService.register(registerDTO);
        return CommonResult.success(userVO);
    }

    @PostMapping("/login")
    public CommonResult<PortalUserVO> login(@Validated @RequestBody PortalLoginDTO loginDTO) {
        log.info("Portal user login: {}", loginDTO.getUsername());
        try {
            PortalUserVO userVO = portalAuthService.login(loginDTO);
            return CommonResult.success(userVO);
        } catch (Exception e) {
            log.error("Login failed", e);
            return CommonResult.failed(e.getMessage());
        }
    }
} 